using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections.ObjectModel;

namespace DC1
{
    public partial class DC1Gui : Form
    {
        public DC1Gui()
        {
            InitializeComponent();
        }

        private void DC1Gui_Load(object sender, EventArgs e)
        {
            Scan();
            dgRequired.Width = this.Width;
            dgOptional.Width = this.Width;
        }

        private void Scan()
        {
            dgOptional.Reset();
            dgRequired.Reset();
            WorkingSplash splash = new WorkingSplash();
            splash.Show();
            btnInstall.Enabled = true;
            DependenciesSection section = ConfigurationManager.GetSection("DCConfigSection") as DependenciesSection;
            Collection<DependencyTest.DependencyBase> dependencies = LoadDependencyConfig(section);
            splash.MaxDependencies = dependencies.Count;
            int current = 0;
            foreach (DependencyTest.DependencyBase d in dependencies)
            {
                splash.ScanningPrompt = "Scanning for " + d.Title;
                splash.ShowCurrentProgress(current++);
                if (!AddDependencyControl(d) && !d.Optional)
                {
                    btnInstall.Enabled = false;
                }

            }
            splash.Close();
        }
        private Collection<DependencyTest.DependencyBase> LoadDependencyConfig(DependenciesSection section)
        {
            int currentOsBuild = (new DependencyTest.Helpers.Check()).GetOsBuild();
            // check for exact match first
            
            int[] osChoices = new int[section.Dependency.Count];
            int currentOsChoice = 0;
            foreach (OSElement os in section.Dependency)
            {
                if (int.Parse(os.BuildNumber) == currentOsBuild)
                {
                    return dostuff(os.Dependency);
                }
                osChoices[currentOsChoice++] = int.Parse(os.BuildNumber);
            }
            Array.Sort(osChoices);
            foreach (int osBuildNumber in osChoices)
            {
                if (osBuildNumber >= currentOsBuild)
                {
                    foreach (OSElement os in section.Dependency)
                    {
                        if (int.Parse(os.BuildNumber) == osBuildNumber)
                        {
                            return dostuff(os.Dependency);
                        }
                    }
                }
            }
            return null;   
        }
        private Collection<DependencyTest.DependencyBase> dostuff(DependencyElementCollection dependencies)
        {
            Collection<DependencyTest.DependencyBase> results = new Collection<DependencyTest.DependencyBase>();
            foreach (DependencyElement i in dependencies)
            {
                if (i.Enabled)
                {
                    DependencyTest.DependencyBase newbase=null;
                    switch (i.DependencyType)
                    {
                        case "Software":
                            newbase = new DependencyTest.SoftwareDependency();
                            break;
                        case "ServerRole":
                            newbase = new DependencyTest.ServerRoleDependency();
                            break;
                        case "ServerFeature":
                            newbase = new DependencyTest.ServerFeatureDependency();
                            break;
                        case "Hotfix":
                            newbase = new DependencyTest.HotFixDependency();
                            break;
                        case "Registry":
                            newbase = new DependencyTest.RegistryDependency();
                            break;
                        case "FileLocation":
                            newbase = new DependencyTest.FileDependency();
                            break;
                        case "Wmi":
                            newbase = new DependencyTest.WmiDependency();
                            break;
                        case "SDK":
                            newbase = new DependencyTest.SDKDependency();
                            break;
                        case "NotSupported":
                            newbase = new DependencyTest.NotSupportedDependency();
                            break;
                    }
                    if (newbase != null)
                    {
                        newbase.Title = i.Title;
                        newbase.Explination = i.Explination;
                        newbase.Config = i.Value;
                        newbase.DownloadUrl = i.DownloadUrl;
                        newbase.InfoUrl = i.InfoUrl;
                        newbase.ScriptName = i.ScriptName;
                        newbase.Optional = i.Optional;
                        results.Add(newbase);
                    }
                }
            }
            return results;
        }

        private bool AddDependencyControl(DependencyTest.DependencyBase dependecy)
        {
            if (dependecy.Optional)
            {
                return dgOptional.AddDependency(dependecy);
            }
            else
            {
                return dgRequired.AddDependency(dependecy);
            }
            //int controlNumber=this.flowLayoutPanel1.Controls.Count+1;
            //DCControls.DependencyViewControl newControl = new DCControls.DependencyViewControl();
            //newControl.Dependency = dependecy;
            ////newControl.BackColor = System.Drawing.Color.Black;
            //// Not needed because the flow layout will layout the control
            //// newControl.Location = new System.Drawing.Point(3, 3);
            //newControl.Name = "dependencyViewControl"+controlNumber.ToString();
            ////newControl.Size = new System.Drawing.Size(524, 69);
            //newControl.TabIndex = controlNumber-1;
            //flowLayoutPanel1.Controls.Add(newControl);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["InstallAction"]);
        }

        private void btnRescan_Click(object sender, EventArgs e)
        {
            Scan();
        }
        
    }
}